<?php

class MockResolver {
  var $m_paths;

  function MockResolver($paths) {
    $this->m_paths = $paths;
  }

  function resolv($name) {
    if (isset($this->m_paths[$name]))  {
      return $this->m_paths[$name];
    } else {
      return null;
    }
  }
}

class MockFactory extends PicoIOCFactory{

  var $m_singleton;

  function & instantiate($name) {
    $this->m_singleton = false;
    switch($name) {
      case 'resolver1':
        $instance = new MockObject();
        $instance->set_foo('first-resolver');
        break;
      case 'resolver2':
        $instance = new MockObject();
        $instance->set_foo('second-resolver');
        break;
      default: 
        echo "[Unknown:$name]";
        $instance = null;
    }
    return $instance;
  }

  function is_singleton() {
    return $this->m_singleton;
  }
}

class IocTest extends UnitTestCase {

  var $m_resolver1;
  var $m_resolver2;
  var $m_ioc;
  var $m_dataPath;
  var $m_shortcuts;

  function setUp() {
    parent::setUp();
    $this->m_resolver1 =& new MockResolver(array(
      'from-first-resolver' => 'resolver1'
    ));
    $this->m_resolver2 =& new MockResolver(array(
      'from-second-resolver' => 'resolver2'
    ));
    $this->m_shortcuts = array(
      'shortcut-no-singleton' => array(
        false, 'MockObject', array( 'foo' => 'foo-ns-shortcut', 'bar' => 'bar-ns-shortcut')
      ),
      'shortcut-singleton' => array(
        true, 'MockObject', array( 'foo' => 'foo-s-shortcut', 'bar' => 'bar-s-shortcut')
      ),
    );
    $resolvers = array($this->m_resolver1, $this->m_resolver2);
    $this->m_ioc =& new PicoIOC($this->m_shortcuts, $resolvers, 'MockFactory');
  }

  function tearDown() {
    $this->m_resolver1 = null;
    $this->m_resolver2 = null;
    $this->m_shortcuts = null;
    $this->m_ioc = null;
    $this->m_dataPath = null;
    parent::tearDown();
  }
 
  function testResolversUsage() {
    $instance =& $this->m_ioc->get('from-first-resolver');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'first-resolver');
    $instance =& $this->m_ioc->get('from-second-resolver');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'second-resolver');
  }

  function testSingletonShortcut() {
    $instance =& $this->m_ioc->get('shortcut-singleton');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'foo-s-shortcut');
    $this->assertIdentical($instance->get_bar(), 'bar-s-shortcut');

    $instance->set_foo('NewValue');

    $instance =& $this->m_ioc->get('shortcut-singleton');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'NewValue');
    $this->assertIdentical($instance->get_bar(), 'bar-s-shortcut');
  }
  
  function testNoSingletonShortcut() {
    $instance =& $this->m_ioc->get('shortcut-no-singleton');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'foo-ns-shortcut');
    $this->assertIdentical($instance->get_bar(), 'bar-ns-shortcut');

    $instance->set_foo('NewValue');

    $instance =& $this->m_ioc->get('shortcut-no-singleton');
    $this->assertNotNull($instance);
    $this->assertIdentical($instance->get_foo(), 'foo-ns-shortcut');
    $this->assertIdentical($instance->get_bar(), 'bar-ns-shortcut');
  }

}
  

?>
